<?php
/**
* @version		$Id: folderlist.php 14401 2010-01-26 14:10:00Z louis $
* @package		Joomla.Framework
* @subpackage	Parameter
* @copyright	Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
* @license		GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/

// Check to ensure this file is within the rest of the framework
defined('JPATH_BASE') or die();

/**
 * Renders a filelist element
 *
 * @package 	Joomla.Framework
 * @subpackage		Parameter
 * @since		1.5
 */

class JElementFolderlist extends JElement
{
	/**
	* Element name
	*
	* @access	protected
	* @var		string
	*/
	var	$_name = 'Folderlist';

	function fetchElement($name, $value, &$node, $control_name)
	{
		jimport( 'joomla.filesystem.folder' );
		// path to images directory
		$path		= JPATH_ROOT.DS.$node->attributes('directory');
		$filter		= $node->attributes('filter');
		$exclude	= $node->attributes('exclude');
		$folders	= JFolder::folders($path, $filter);
        $filter     = '\.png$|\.gif$|\.jpg$|\.bmp$|\.ico$';
        
        $script  = array();
		$options = array ();       
        $script[]  = "var list = new Array();";
        $i = 1;
        $files     = JFolder::files(JPATH_ROOT.DS.'images', $filter);
        $files[count($files)-1] .= '=\"title|desc\"';
        $script[]  = "list[0] = new Array(\"".$node->attributes('directory')."\",\"".implode('=\"title|desc\"\n',$files)."\");";
        
		foreach ($folders as $index => $folder)
		{
			if ($exclude)
			{
				if (preg_match( chr( 1 ) . $exclude . chr( 1 ), $folder )) {
					continue;
				}
			}
            
            $files     = JFolder::files($path.DS.$folder, $filter);
            $files[count($files)-1] .= '=\"title|desc\"';
            $script[]  = "list[{$i}] = new Array(\"".$node->attributes('directory').'/'.$folder."\",\"".implode('=\"title|desc\"\n',$files)."\");";
			$options[] = JHTML::_('select.option', $node->attributes('directory').'/'.$folder, $folder);
            $i++;
		}
        
        $script[]  = '
                function changeValue(selector, source, value){
                    if(value == ""){
                        $(\'\'+selector).value = "";
                        return;
                    } 
                    for (x in source) {
                		if (source[x][0] == value) {
          		            $(\'\'+selector).value = source[x][1];
                		}
                	}                    
                }
        ';
        array_unshift($options, JHTML::_('select.option', 'images', 'Images'));
		array_unshift($options, JHTML::_('select.option', '', '- '.JText::_('None Selected').' -'));
        $document = JFactory::getDocument();
        $document->addScriptDeclaration(implode("\n",$script));
        
		return JHTML::_('select.genericlist',  $options, ''.$control_name.'['.$name.']', 'class="inputbox"', 'value', 'text', $value, $control_name.$name);
	}
}
